package com.usts.edu.studenttemp.controller;

import com.usts.edu.studenttemp.config.SystemVariable;
import com.usts.edu.studenttemp.dto.*;
import com.usts.edu.studenttemp.service.TemperatureService;
import com.usts.edu.studenttemp.utils.ResultResponse;
import com.usts.edu.studenttemp.utils.ResultTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Guanzhong Hu
 * Date :2020/5/31
 * Description : 体温
 * Version :1.0
 */
@RestController
@RequestMapping("/api/v1/temp")
public class TemperatureController extends BaseController {

    @Autowired
    private TemperatureService temperatureService;
    @Autowired
    private JavaMailSender javaMailSender;

    @ResponseBody
    @RequestMapping("save")
    public ResultResponse save(Integer tempid, String temp) {


        // 通过JAVAMAIL 发送邮件 如果体温大于37.5
        if (Double.parseDouble(temp) > SystemVariable.MAX_STUDENT_TEMP) {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setFrom("13605149648@163.com");
            mailMessage.setTo("594243471@qq.com");
            mailMessage.setSubject("有同学体温过高");
            mailMessage.setText("" +
                    "学号:" + getUser().getUserNumber() +
                    "\t姓名:" + getUser().getUserName() +
                    "，体温过高为:" + temp + "℃,请老师予以查证！");
            javaMailSender.send(mailMessage);
        }
        if (StringUtils.isEmpty(tempid)) {
            if (temperatureService.insertSelectiveTemperature(temp, getUserId()) == SystemVariable.ERROR)
                return ResultResponse.Build(ResultTypeEnum.fail, SystemVariable.DO_ERROR);
        } else {
            Temperature temperature = new Temperature();
            temperature.setId(tempid);
            temperature.setTemp(temp);
            temperature.setCreatetime(new Date());

            if (temperatureService.updateByPrimaryKeySelective(temperature) == SystemVariable.ERROR)
                return ResultResponse.Build(ResultTypeEnum.fail, SystemVariable.DO_ERROR);
        }
        return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
    }

    @RequestMapping("gettemp")
    @ResponseBody
    public ResultResponse gettemp() {
        return ResultResponse.Build(ResultTypeEnum.success, temperatureService.selectStudentTempByStuid(getUserId()));
    }

    @ResponseBody
    @RequestMapping("showbyid")
    public Map<String, Object> showById(ResqStudentTemperature stemperature) {
        stemperature.setSid(getUserId());
//        {"code":0,"msg":"","count":1000,"data"
        Map<String, Object> so = new HashMap<String, Object>();
        List<StudentTemperature> ls = temperatureService.selectTemperatureByUserId(stemperature);
        if (StringUtils.isEmpty(getUserId())) {
            so.put("code", SystemVariable.URL_FAIL);
            so.put("msg", "登录状态失效或未登录！");
        } else {
            so.put("code", SystemVariable.URL_SUCCESS);
            so.put("msg", "");
        }
        so.put("count", ls.size());

        so.put("data", ls);
        return so;
    }

    @ResponseBody
    @RequestMapping("showstudenttemp")
    public Map<String, Object> showStudentTemp(AdminStudentTemperature studentTemperature){
        Map<String, Object> so = new HashMap<String, Object>();
        List<AdminStudentTemperatureShow> ls = temperatureService.selectAllStudentTemp(studentTemperature);
        if (StringUtils.isEmpty(getUserId())) {
            so.put("code", SystemVariable.URL_FAIL);
            so.put("msg", "登录状态失效或未登录！");
        } else {
            so.put("code", SystemVariable.URL_SUCCESS);
            so.put("msg", "");
        }
        so.put("count", ls.size());

        so.put("data", ls);
        return so;
    }

}
